package com.sinoma.G2project.MoYu.app;

import com.alibaba.fastjson.TypeReference;
import com.firenay.common.constant.AuthServerConstant;
import com.firenay.common.exception.BizCodeEnum;
import com.firenay.common.utils.R;
import com.firenay.common.vo.MemberRsepVo;
import com.sinoma.G2project.MoYu.Service.MemberService;
import com.sinoma.G2project.MoYu.entity.MemberEntity;
import com.sinoma.G2project.MoYu.vo.UserLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * <p>Title: LoginController</p>
 * Description：登录注册模块
 * date：2020/6/25 13:02
 */
@Slf4j
@Controller
public class LoginController {

    @Autowired
    MemberService memberService;

    @GetMapping({"/login.html", "/", "/index", "/index.html"})
    public String loginPage(HttpSession session, Model model) {
        Object attribute = session.getAttribute(AuthServerConstant.LOGIN_USER);
        if (attribute == null) {
            return "login";
        }
        MemberRsepVo attribute1 = (MemberRsepVo) attribute;
        HashMap<String, String> userInfo = new HashMap<>();
        userInfo.put("username",attribute1.getUsername());
        model.addAttribute("userInfo",userInfo);
        return "index";
    }


    @GetMapping({"/register","/register.html"})
    public String registerPage(HttpSession session) {
        Object attribute = session.getAttribute(AuthServerConstant.LOGIN_USER);
        if (attribute == null){
            return "login";
        }
        MemberRsepVo vo = (MemberRsepVo) attribute;
        if ("admin".equals(vo.getUsername())) {
            return "register";
        }
        return "login";
    }

    @PostMapping("/login")
    public String login(UserLoginVo userLoginVo, RedirectAttributes redirectAttributes, HttpSession session) {
        // 登录
        R r;

        MemberEntity memberEntity = memberService.login(userLoginVo);
        if (memberEntity != null) {
            r = R.ok().setData(memberEntity);
        } else {
            r = R.error(BizCodeEnum.LOGINACTT_PASSWORD_ERROR.getCode(), BizCodeEnum.LOGINACTT_PASSWORD_ERROR.getMsg());
        }

        if (r.getCode() == 0) {
            // 登录成功
            MemberRsepVo rsepVo = r.getData("data", new TypeReference<MemberRsepVo>() {
            });
            session.setAttribute(AuthServerConstant.LOGIN_USER, rsepVo);
            HashMap<String, String> userInfo = new HashMap<>();
            userInfo.put("username",rsepVo.getUsername());
            redirectAttributes.addFlashAttribute("userInfo",userInfo);
            log.info("\n欢迎 [" + rsepVo.getUsername() + "] 登录");
            Cookie cookie = new Cookie("G2cookie", "G2cookie");
            cookie.setMaxAge(60 * 60 * 12);
            return "redirect:/MoYu/index";
        } else {
            HashMap<String, String> error = new HashMap<>();
            // 获取错误信息
            error.put("msg", r.getData("msg", new TypeReference<String>() {
            }));
            redirectAttributes.addFlashAttribute("errors", error);
            return "redirect:login.html";
        }
    }

    @GetMapping({"/logout"})
    public String logout(HttpSession session) {
        session.removeAttribute(AuthServerConstant.LOGIN_USER);
        return "login";
    }

}
